/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.admin;

import entity.ContactCompany;
import entity.Message;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import session.ContactCompanyFacade;
import utility.Page;

/**
 *
 * @author owtee
 */
public class ContactCompanyGetAll extends HttpServlet {

    @EJB
    private ContactCompanyFacade contactCompanyFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            try {
                String txtConComNameKey = (request.getParameter("txtConComNameKey") != null
                        ? request.getParameter("txtConComNameKey")
                        : "");

                int pageNo = (request.getParameter("page_no") != null
                        && request.getParameter("page_no").length() > 0
                        ? Integer.parseInt(request.getParameter("page_no"))
                        : 0);

                ServletContext context = getServletContext();
                int ITEM_PER_PAGE = Integer.parseInt(context.getInitParameter("ITEM_PER_PAGE"));

                List<ContactCompany> conComLstTmp = contactCompanyFacade.findByName("%" + txtConComNameKey + "%");
                int numRecord = conComLstTmp.size();
                Page page = new Page(numRecord, pageNo, ITEM_PER_PAGE);

                List<ContactCompany> conComLst = contactCompanyFacade.findByName(page.getFromIndex(),
                        ITEM_PER_PAGE,
                        "%" + txtConComNameKey + "%");
                // Set new line to show at screen
                for (ContactCompany conComI : conComLst) {
                    conComI.setConComAddress(conComI.getConComAddress().replace("\n", "<br/>"));
                }
                int pageCount = page.size();

                request.setAttribute("conComLst", conComLst);
                request.setAttribute("pageCount", pageCount);
                request.setAttribute("txtDepartmentKey", txtConComNameKey);
                request.setAttribute("pageNo", pageNo);


            } catch (Exception ex) {
                Message message = new Message();
                message.setMesDetail(ex.getMessage());
                request.setAttribute("message", message);
            }

            RequestDispatcher rd = request.getRequestDispatcher("contact_company_all.jsp");
            rd.forward(request, response);

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
